package com.hankol.material.adapter

import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import androidx.lifecycle.LifecycleOwner
import com.baselibrary.MessageBus
import com.baselibrary.net.APIResponse
import com.baselibrary.permission.PermissionCollection
import com.baselibrary.permission.PermissionsUtils
import com.baselibrary.permission.callback.PermissionsCall
import com.baselibrary.utils.*
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.material.R
import com.hankol.material.api.MaterialConstants
import com.hankol.material.bean.CreateFolderBean
import com.hankol.material.bean.MaterialBusinessBean
import com.hankol.material.bean.MaterialContentInfo
import com.hankol.material.bean.TabConfigure
import com.baselibrary.bean.download.ShowUIUtils
import com.hankol.material.fragment.ContentListFragment
import com.hankol.material.viewmodel.MaterialContentViewModel


/**
 * @description:
 * @author: 关忆北丶
 * @date :   2021/11/17 13:16
 */
class ContentListFolderAdapter(private var viewModel: MaterialContentViewModel,
                               private var activity: ContentListFragment, private var owner: LifecycleOwner,
                               private var bean: MaterialBusinessBean) : BaseQuickAdapter<MaterialContentInfo.FileLibsBean.DataBean, BaseViewHolder>(R.layout.item_contentlist_folder) {

    var collectionFolderId: Long = 0
    private var position: Int = -1
    override fun convert(helper: BaseViewHolder, item: MaterialContentInfo.FileLibsBean.DataBean) {
        item.collectionFolderId = collectionFolderId
        GlideUtils.loadImg_101(mContext, item.thumbnail, helper.getView(R.id.iv_contentFolder_cover))
        if (TextUtils.isEmpty(bean.searchContent)) {
            helper.setText(R.id.tv_contentFolder_title, "${item.name}.${item.desc}")
        } else {
            helper.setText(R.id.tv_contentFolder_title, ColorTextUtils.setKeyWordColor(mContext, "${item.name}.${item.desc}", bean.searchContent, R.color.color_c39d67))
        }
        helper.setText(R.id.tv_contentFolder_size, ShowUIUtils.byteToString(item.fileSize))

        helper.itemView.setOnClickListener {
            if (!HKUtils.isFastClick()) return@setOnClickListener
            //文件详情
            item.isFromCollect = item.collectionFolderId > 0//是否来自收藏夹
            MaterialConstants.openDocumentDetail(item)
            position = helper.adapterPosition

        }
        helper.getView<ImageView>(R.id.iv_contentFolder_more).setOnClickListener {

            viewModel.showDownloadFile(item.isHasCollect == 1, bean, "${item.name}.${item.desc}", activity.attachActivity, View.OnClickListener { it ->
                when (it.id) {
                    R.id.ll_file_download -> {
                        if (!HKUtils.isFastClick()) return@OnClickListener
                        PermissionsUtils.getInstance().chekPermissions(activity.attachActivity, PermissionCollection.FILE.getPermissionList(), object : PermissionsCall() {
                            override fun permissionsPass() {
                                //下载
                                viewModel.reqMaterialAuthenticity(item.id, object : APIResponse<Boolean>(this) {
                                    override fun onSuccess(result: Boolean?, json: String?) {
                                        viewModel.reqAddShareCount(item.id,0)
                                        MaterialConstants.downloadFile(activity.attachActivity, item.fileUrl, "${item.name}.${item.desc}", item.thumbnail,item.fileSize, item.type)
                                    }
                                })
                            }
                        })

                    }
                    R.id.ll_file_det -> {
                        if (!HKUtils.isFastClick()) return@OnClickListener
                        viewModel.loadCancelCollection(item.collectionFolderId, item.id, object : APIResponse<Boolean>(this) {
                            override fun onSuccess(result: Boolean?, json: String?) {
                                ToastUtils.showShort(mContext, "删除成功")
                                position = helper.adapterPosition
                                EventBusUtils.sendNotify1Value(MessageBus.msgId_material_moveCollection, TabConfigure.FILE.tabConfig)
                                EventBusUtils.sendNotify1Value(MessageBus.msgId_material_detCollection, TabConfigure.FILE.tabConfig)
                            }

                            override fun onFailed(message: String?) {
                                ToastUtils.showShort(mContext, "网络异常，删除失败")
                            }
                        })
                    }

                    R.id.ll_file_collection -> {
                        if (!HKUtils.isFastClick()) return@OnClickListener
                        if (item.isHasCollect == 0) {
                            viewModel.collectionMaterial(activity.attachActivity, owner, item.id, object : APIResponse<CreateFolderBean>(this) {
                                override fun onSuccess(result: CreateFolderBean?, json: String?) {
                                    ToastUtils.showShort(mContext, "收藏成功")
                                    item.isHasCollect = 1
                                }

                                override fun onFailed(message: String?) {
                                    ToastUtils.showShort(mContext, "网络异常，收藏失败")
                                }
                            })
                        } else {
                            viewModel.loadCancelCollection(item.collectionFolderId, item.id, object : APIResponse<Boolean>(this) {
                                override fun onSuccess(result: Boolean?, json: String?) {
                                    ToastUtils.showShort(mContext, "已取消收藏")
                                    item.isHasCollect = 0
                                    if (collectionFolderId > 0) {
                                        data.removeAt(helper.adapterPosition)
                                        notifyDataSetChanged()
                                        EventBusUtils.sendNotify1Value(MessageBus.msgId_material_moveCollection, TabConfigure.FILE.tabConfig)
                                    }
                                }

                                override fun onFailed(message: String?) {
                                    ToastUtils.showShort(mContext, "网络异常，取消收藏失败")
                                }
                            })
                        }
                    }

                    R.id.ll_file_move -> {
                        if (!HKUtils.isFastClick()) return@OnClickListener
                        viewModel.moveMaterial(activity.attachActivity, owner, item.id, item.collectionFolderId, object : APIResponse<Boolean>(this) {
                            override fun onSuccess(result: Boolean?, json: String?) {
                                ToastUtils.showShort(mContext, "移动成功")
                                position = helper.adapterPosition
                                EventBusUtils.sendNotify1Value(MessageBus.msgId_material_moveCollection, TabConfigure.FILE.tabConfig)
                                EventBusUtils.sendNotify1Value(MessageBus.msgId_material_detCollection, TabConfigure.FILE.tabConfig)
                            }

                            override fun onFailed(message: String?) {
                                ToastUtils.showShort(mContext, "网络异常，移动失败")
                            }
                        })
                    }

                    R.id.ll_file_share -> {
                        if (!HKUtils.isFastClick()) return@OnClickListener
                        viewModel.shareMaterial(activity.attachActivity, item.id, item.name, item.thumbnail, true, TabConfigure.FILE.tabConfig)
                    }
                }
            })
        }
    }

    fun removeCurrentData() {
        if (position != -1) {
            data.removeAt(position)
            notifyDataSetChanged()
        }
    }

    fun upItemData(isHasCollect: Int) {
        if (position > -1) {
            data[position].isHasCollect = isHasCollect
        }
    }

}
